//
//  PopScreeningView.m
//  NoneCar
//
//  Created by DongSen on 2018/7/17.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "PopScreenCollectionViewCell.h"
#import "PopScreeningView.h"
@interface PopScreeningView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic)UICollectionReusableView * headView;
@end
@implementation PopScreeningView
+ (PopScreeningView*)loadView{
    NSArray * viewArray =[[NSBundle mainBundle] loadNibNamed:@"PopScreeningView" owner:self options:nil];
    PopScreeningView * view  = [viewArray firstObject];
    [view.dataArray addObjectsFromArray:@[@"待查勘",@"待定损",@"定损审核中",@"定损审核不通过",@"已定损",@"已注销",@"无法定损，待公估"]];
    view.collectionView.delegate = view;
    view.collectionView.dataSource = view;
    view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [view.collectionView registerNib:[UINib nibWithNibName:@"PopScreenCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PopScreenCollectionViewCell"];
    [view.collectionView  registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
    view.contentHeight.constant = 44;
    return view;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size = CGSizeMake(kSCREEN_W, 40);
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
        if (!self.headView) {
            self.headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
            UILabel * label = [UILabel new];
            label.frame = CGRectMake(16,10, kSCREEN_W, 20);
            label.text = @"状态筛选（可多选）";
            label.font = Font(12);
            label.textColor = STRCOLOR(@"#999999");
            [self.headView addSubview:label];
        }
        return self.headView;
    }
    return reusableview;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"选中%ld按钮",indexPath.row);
    PopScreenCollectionViewCell *cell = (PopScreenCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if ([self.selectArray containsObject:self.dataArray[indexPath.row]]) {
        [self.selectArray removeObject:self.dataArray[indexPath.row]];
        cell.ShowSelected = NO;
    }
    else{
        [self.selectArray addObject:self.dataArray[indexPath.row]];
        cell.ShowSelected = YES;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PopScreenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PopScreenCollectionViewCell" forIndexPath:indexPath];
    cell.titlesLabel.text = self.dataArray[indexPath.row];
    if ([self.selectArray containsObject:self.dataArray[indexPath.row]]) {
        cell.ShowSelected = YES;
    }
    else{
        cell.ShowSelected = NO;
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(collectionView.frame.size.width/2-24, 38);
}
- (IBAction)clearButtonClicked:(UIButton *)sender{
    [self.selectArray removeAllObjects];
    [self.collectionView reloadData];
//    self.block();
}
- (IBAction)completeButtonClicked:(UIButton *)sender{
    if (self.selectArray.count<1) {
        return;
    }
    __block NSString * str;
    [self.selectArray enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx==0) {
            str = obj;
        }
        else{
            str = [NSString stringWithFormat:@"%@,%@",str,obj];
        }
    }];
    NSLog(@"%@",self.selectArray);
    if (self.block) {
        self.block(str);
    }
    [self removieView];
}

- (void)showView{
    self.show = YES;
    [self layoutIfNeeded];

    [self.collectionView reloadData];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.contentHeight.constant = 44;
    [UIView animateWithDuration:0.2 animations:^{
        self.contentHeight.constant = 280;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}

- (void)removieView{
    self.show = NO;
    if (self.disblock) {
        self.disblock();
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.contentHeight.constant = 44;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (IBAction)closeButton:(UIButton *)sender{
    [self removieView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
